package com.guducat.collegeWeb.entity;

import lombok.Data;

import java.time.LocalDateTime;

/**
 * 用户数据同步日志实体类
 * 对应 user_data_sync_log 表
 */
@Data
public class UserDataSyncLog {

    /**
     * 主键ID
     */
    private Long id;

    /**
     * 用户ID
     */
    private Integer userId;

    /**
     * 同步类型：upload(上传)、download(下载)
     */
    private String syncType;

    /**
     * 数据类型：favorite、history、like
     */
    private String dataType;

    /**
     * 同步数据条数
     */
    private Integer syncCount;

    /**
     * 冲突数据条数
     */
    private Integer conflictCount;

    /**
     * 成功处理条数
     */
    private Integer successCount;

    /**
     * 客户端版本
     */
    private String clientVersion;

    /**
     * 同步状态：processing、success、failed
     */
    private String syncStatus;

    /**
     * 错误信息
     */
    private String errorMessage;

    /**
     * 同步时间
     */
    private LocalDateTime syncTime;

    /**
     * 同步类型常量
     */
    public static class SyncType {
        public static final String UPLOAD = "upload";
        public static final String DOWNLOAD = "download";
        public static final String MERGE = "merge";
    }

    /**
     * 同步状态常量
     */
    public static class SyncStatus {
        public static final String PROCESSING = "processing";
        public static final String SUCCESS = "success";
        public static final String FAILED = "failed";
    }

    /**
     * 构造方法
     */
    public UserDataSyncLog() {
        this.syncTime = LocalDateTime.now();
        this.syncStatus = SyncStatus.PROCESSING;
    }

    public UserDataSyncLog(Integer userId, String syncType, String dataType) {
        this();
        this.userId = userId;
        this.syncType = syncType;
        this.dataType = dataType;
    }

    public UserDataSyncLog(Integer userId, String syncType, String dataType, String clientVersion) {
        this(userId, syncType, dataType);
        this.clientVersion = clientVersion;
    }

    /**
     * 标记为成功
     */
    public void markSuccess() {
        this.syncStatus = SyncStatus.SUCCESS;
    }

    /**
     * 标记为失败
     */
    public void markFailed(String errorMessage) {
        this.syncStatus = SyncStatus.FAILED;
        this.errorMessage = errorMessage;
    }

    /**
     * 设置统计数据
     */
    public void setStats(Integer syncCount, Integer successCount, Integer conflictCount) {
        this.syncCount = syncCount;
        this.successCount = successCount;
        this.conflictCount = conflictCount;
    }

    /**
     * 计算失败数量
     */
    public Integer getFailedCount() {
        if (syncCount == null || successCount == null) {
            return 0;
        }
        return syncCount - successCount;
    }

    @Override
    public String toString() {
        return "UserDataSyncLog{" +
                "id=" + id +
                ", userId=" + userId +
                ", syncType='" + syncType + '\'' +
                ", dataType='" + dataType + '\'' +
                ", syncCount=" + syncCount +
                ", conflictCount=" + conflictCount +
                ", successCount=" + successCount +
                ", clientVersion='" + clientVersion + '\'' +
                ", syncStatus='" + syncStatus + '\'' +
                ", errorMessage='" + errorMessage + '\'' +
                ", syncTime=" + syncTime +
                '}';
    }
}
